from flask_restplus import Resource, Namespace, fields, marshal, reqparse
from common.decorators import catch_error
from app.storage.storage_record import service
from app.user.decorators import token_required
from common.responses import ok
from flask import g
from common import constant
from common.response_model import base_model, pagination_model
from common.request_parser import get_parser as base_get_parser
from app.product.sku.controller import sku_model as simple_sku_model
from app.product.unit.controller import unit_model
from app.storage.storage_record import dao
from app.product.sku.model import Sku
from app.product.unit.model import Unit
from app.storage.storage_record.model import StorageRecord
api = Namespace('storage_record')

# ---------- model ----------
storage_record_model = base_model.inherit('storage_record_model', {
    "sku_id": fields.Integer(description='sku id'),
    "unit_id": fields.Integer(description='unit id'),
    "warehouse_id": fields.Integer(description='warehouse id'),

    "num": fields.Float(description='数量'),
    'cost_price': fields.Float(description='库存成本单价'),

    "sku": fields.Nested(simple_sku_model),
    "unit": fields.Nested(unit_model),
    "warehouse": fields.Nested(unit_model),
})
storage_record_paginate_model = pagination_model.inherit('storage_record_paginate_model', {
    "items": fields.Nested(storage_record_model)
})

# ---------- create parser ----------
# ---------- get parser ----------
# 单仓库， 合并仓， 分仓库显示
get_parser = base_get_parser.copy()
get_parser.replace_argument('search_str', type=str,
                            help='搜索，支持正则 sku_name, sku_sn, sku_short_name')
get_parser.add_argument('warehouse_id', help='warehouse_id', type=int)
get_parser.add_argument('sku_id', help='sku_id', type=int)
get_parser.add_argument('unit_id', help='unit_id', type=int)
get_parser.replace_argument('sorted_by', type=str, choices=[
    'name', 'id'
], default='id', help='sorted_by')


# ---------- put parser ----------


class StorageRecordsResource(Resource):
    @catch_error
    @token_required
    @api.expect(get_parser)
    def get(self):
        """获取列表"""
        args = get_parser.parse_args()

        pagination = dao.get_by_page(**args)
        new_items = []
        for ele in pagination['items']:
            storage_record = StorageRecord.query.filter_by(id=ele['id']).first()
            new_items.append(marshal(storage_record, storage_record_model))

        pagination['items'] = new_items
        return ok(data=pagination,
                  message='get success')


class StorageRecordResource(Resource):
    @catch_error
    @token_required
    def get(self, obj_id):
        """获取"""
        obj = service.get(g.user_id, obj_id)
        return ok(data=marshal(obj, storage_record_model),
                  message='get success')


# --------- route ----------
api.add_resource(StorageRecordsResource, '/storage_records')
api.add_resource(StorageRecordResource, '/storage_record/<int:obj_id>')

# 注册 response 文档
api.models[storage_record_model.name] = storage_record_model
